package yxy.game.pm2.command.soul;

import yxy.apple.util.RandomUtility;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.souldata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.SoulHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.ArrayList;
import java.util.List;

/** 魂玉 魂玉合成 */
@MessageType(proto = Command.SoulCompound, classtype = HeroMessage.SoulCompoundRequest.class)
public class CMD_SoulCompound extends Command<HeroMessage.SoulCompoundRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.SoulCompoundRequest request) {
		String playerCode = messageSource.getTag();
		List<String> codes = request.getCodesList();

		HeroMessage.SoulCompoundResponse.Builder response = HeroMessage.SoulCompoundResponse.newBuilder();

		// 检查所有的魂玉
		souldata data = null;
		ArrayList<Soul> souls = new ArrayList<>();
		for (String code : codes) {
			Soul soul = Soul.get(playerCode, code);
			if (data == null) {
				data = DataCenter.getData(soul.getDataId(), souldata.class);
			}
			if (soul == null || soul.getQuality() != data.getQuality()) {
				send(messageSource, response, Result.ParamError);
				return;
			}
			souls.add(soul);
		}

		souldata nextdata = DataCenter.getData(data.getUpgradeid(), souldata.class);
		if (data == null || nextdata == null || data.getUpheave() != 1) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		Currency cost = Currency.create(CurrencyType.CT_钱币, 0, data.getUpcost());
		try {
			new CurrencyHandle().cost(playerCode, CCReason.魂玉, cost);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		}

		int rate = 0;
		switch (souls.size()) {
		case 2:
			rate = data.getRate2();
			break;
		case 3:
			rate = data.getRate3();
			break;
		case 4:
			rate = data.getRate4();
			break;
		case 5:
			rate = data.getRate5();
			break;
		default:
			send(messageSource, response, Result.ParamError);
			return;
		}

		try {
			if (RandomUtility.ppm() > rate) {
				// 合成失败 发补偿
				Currency ret = Currency.create(data.getReturndestype(), data.getReturndesid(), data.getReturndesnum() * souls.size());
				new CurrencyHandle().addCurrency(playerCode, CCReason.魂玉, ret);

				response.addItem(new CurrencyHandle().packItem(ret));
				response.setSuccess(false);
			} else {
				// 合成成功
				Soul soul = Soul.create(nextdata, playerCode);

				response.setSoul(new SoulHandle().packSoul(soul));
				response.setSuccess(true);
			}
			response.addAllCodes(codes);
			send(messageSource, response, Result.Success);

			Soul.remove(playerCode, souls);

			TaskHandle.handle(playerCode, TaskType.统计型_工坊_龙魂合成_次数, 1);
		} catch (RedisException e) {
//			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, HeroMessage.SoulCompoundResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}